/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       clouds.cpp
 * Author:     karooolek
 * Created on: Jan 4, 2010
 *
 **********************************************************************************************************************/

#include "clouds.h"

namespace mGameEngine
{
namespace Game
{

using namespace Graphics;

Clouds::Clouds() :
    Drawable(),
    _texture(NULL), _density(1.0f),
    _height(1.0f), _size(1.0f), _velocity(),
    _sunColor(1.0f, 1.0f, 1.0f, 1.0f), _sunDirection(0.0f, 0.0f, -1.0f),
    _penetration(0.1f)
{
    _boundingSphere.setRadius(999999999999.9f);
}

//Clouds::Clouds(const Texture *texture,
//    float height, float size, const Vector2 &velocity,
//    const Color &sunColor, const Vector3 &sunPosition) :
//    Drawable(),
//    _texture(texture),
//    _height(height), _size(size), _velocity(velocity),
//    _sunColor(sunColor), _sunPosition(sunPosition),
//    _penetration(0.1f)
//{
//}

Clouds::~Clouds()
{
}

void Clouds::draw() const
{
    static GPUProgram gpuProgram(
#include "clouds.vert"
            ,
#include "clouds.frag"
            , true);

    // bind gpu program
    gpuProgram.use();
    gpuProgram.setValue("t", _timer.getTime());
    gpuProgram.setTexture("texture", _texture, 0);
    gpuProgram.setValue("density", _density);
    gpuProgram.setValue("height", _height);
    gpuProgram.setValue("size", _size);
    gpuProgram.setVector("velocity", _velocity);
    gpuProgram.setVector("sunColor", Vector4(_sunColor));
    gpuProgram.setVector("sunDirection", _sunDirection);
    gpuProgram.setValue("penetration", _penetration / _size);

    // render cloud layer
    static uint dl = 0;
    if(!dl)
    {
        dl = glGenLists(1);
        glNewList(dl, GL_COMPILE);

        glDisable(GL_DEPTH_TEST);

        glBegin(GL_QUADS);
            glVertex3fv(Vector3(-1000.0f, 1000.0f, 1.0f));
            glVertex3fv(Vector3(1000.0f, 1000.0f, 1.0f));
            glVertex3fv(Vector3(1000.0f, -1000.0f, 1.0f));
            glVertex3fv(Vector3(-1000.0f, -1000.0f, 1.0f));
        glEnd();

        glEnable(GL_DEPTH_TEST);

        glEndList();
    }
    glCallList(dl);
}

}
}


